package com.zzyl.framework.interceptor.impl;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * ClassName: MyMetaObjectHandler
 * Package: com.zzyl.framework.interceptor.impl
 * Description:
 *
 * @Author 陈宽
 * @Create 2025/5/21 9:22
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"createTime",Date.class,new Date());
        strictInsertFill(metaObject,"createBy",String.class,loginUser());
    }
    private String loginUser(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(loginUser == null){
            return "1";
        }
        return loginUser.getUserId().toString();
    }

    @Override
    public void updateFill(MetaObject metaObject) {
//        strictUpdateFill(metaObject,"updateTime",Date.class,new Date());
//        strictUpdateFill(metaObject,"updateBy",String.class,loginUser());
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", loginUser(), metaObject);
    }
}
